{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "dd53f484",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "sess = tf.InteractiveSession()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "2f05cc86",
   "metadata": {},
   "outputs": [],
   "source": [
    "def weight_variable(shape):\n",
    "    #initial = tf.truncated_normal(shape, stddev=0)\n",
    "    initial = tf.constant(1.0, shape=shape)\n",
    "    \n",
    "    with tf.Session() as sess:\n",
    "        print(sess.run(initial))\n",
    "        \n",
    "    return tf.Variable(initial)\n",
    "\n",
    "def bias_variable(shape):\n",
    "    initial = tf.constant(0.0, shape=shape)\n",
    "    \n",
    "    with tf.Session() as sess:\n",
    "        print(sess.run(initial))\n",
    "        \n",
    "    return tf.Variable(initial)\n",
    "\n",
    "def conv2d(x, W):\n",
    "    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')\n",
    "\n",
    "def max_pool_2x2(x):\n",
    "    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2,1], padding='VALID')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "671377fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[1.]]\n",
      "\n",
      "  [[1.]]\n",
      "\n",
      "  [[1.]]]\n",
      "\n",
      "\n",
      " [[[1.]]\n",
      "\n",
      "  [[1.]]\n",
      "\n",
      "  [[1.]]]\n",
      "\n",
      "\n",
      " [[[1.]]\n",
      "\n",
      "  [[1.]]\n",
      "\n",
      "  [[1.]]]]\n",
      "<tf.Variable 'Variable_19:0' shape=(3, 3, 1, 1) dtype=float32_ref>\n"
     ]
    }
   ],
   "source": [
    "w_conv = weight_variable([3,3,1,1])\n",
    "print(w_conv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "d07854a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.]\n",
      "<tf.Variable 'Variable_20:0' shape=(1,) dtype=float32_ref>\n"
     ]
    }
   ],
   "source": [
    "b_conv = bias_variable([1])\n",
    "print(b_conv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "04f08da0",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([[[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14],[15,16,17,18,19],[20,21,22,23,24]]],'float32')\n",
    "x_image = tf.reshape(x,[-1,5,5,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "ada19d04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"add_8:0\", shape=(1, 3, 3, 1), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "conv = conv2d(x_image, w_conv) + b_conv\n",
    "print(conv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "0dcb20e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[ 54.]\n",
      "   [ 63.]\n",
      "   [ 72.]]\n",
      "\n",
      "  [[ 99.]\n",
      "   [108.]\n",
      "   [117.]]\n",
      "\n",
      "  [[144.]\n",
      "   [153.]\n",
      "   [162.]]]]\n"
     ]
    }
   ],
   "source": [
    "sess.run(tf.global_variables_initializer())\n",
    "result = sess.run(conv)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4200ddc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf-1",
   "language": "python",
   "name": "tf-1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
